package com.czk.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * netty的服务端
 *
 * @Author:ChenZhangKun
 * @Date: 2021/6/1 14:07
 */
public class NettyServer {
    public static void main(String[] args) {
        ServerBootstrap bootstrap = new ServerBootstrap();
        NioEventLoopGroup workGroup = new NioEventLoopGroup();
        NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
        ChannelFuture channelFuture = null;
        try {
            bootstrap.group(bossGroup, workGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .childHandler(new MyServerInitializer());
            channelFuture = bootstrap.bind(8080).syncUninterruptibly();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (null == channelFuture || !channelFuture.isSuccess()) {
                System.out.println("服务启动失败");
            } else {
                System.out.println("服务启动成功");
            }
        }

    }
}
